#include "ttsys.h"
#include "Appcli.h"
#include <atmi.h>
#include <fml32.h>
#include<stdlib.h>
#include<time.h>

static 	FBFR32   	*sndBuf = NULL, *rcvBuf = NULL;
static 	FLDID32  	Fml32Id;
static 	FLDOCC32  	Fml32Oc = -1;
static	int 		baseId = 8000;
static	int			FLAG = 0;
static	time_t 		timep;
static  struct 		tm *local_time;
static	char 		data_YYMMDD[8+1];
static	char 		time_HHMMSS[6+1];
static	char		time_YMDHMS[14+1];
static	char 		ref_num[20+1];
MY_YB_100090_IN		*in_100090;
MY_YB_100093_OUT	out_100093;
MY_YB_100091_OUT	out_100091;
MY_YB_100092_OUT	out_100092;

static 	int did_100093[]={64,338,191,226,227,248,129,87,82,432,66,150,74,216,224,225,433,229,222};
static	int	did_100091[]={87};
static	int	did_100092[]={87};
//关键域信息设置
int	 set_key_data()
{
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_CHANNEL_ID + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"0001",4) < 0)
	{
		printf("Fchg32 err [%s]\n","0001");
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_BRANCH_ID + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"00800014",8) < 0)
	{
		printf("Fchg32 err [%s]\n","00800014");
		return -1;
	}
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_TERMINAL_ID + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"13262633583", 11) < 0)
	{
		printf("Fchg32 err [%s]\n","13262633583");
		return -1;
	}
	/*发送时间*/
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_SND_TX_DATE + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,data_YYMMDD,8) < 0)
	{
		printf("Fchg32 err [%s]\n",data_YYMMDD);
		return -1;
	}	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_SND_TX_TIME + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,time_HHMMSS,6) < 0)
	{
		printf("Fchg32 err [%s]\n",time_HHMMSS);
		return -1;
	}
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_SND_TX_CYCLE + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,data_YYMMDD,8) < 0)
	{
		printf("Fchg32 err [%s]\n",data_YYMMDD);
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_SND_TX_LOGNO + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"000666",6) < 0)
	{
		printf("Fchg32 err [%s]\n","000666");
		return -1;
	}

	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_ORG_TX_REF_NO + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,ref_num,20) < 0)
	{
		printf("Fchg32 SYS_FLD_ORG_TX_REF_NO err [%s]\n",ref_num);
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_ORG_TX_DATE + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,data_YYMMDD,8) < 0)
	{
		printf("Fchg32  SYS_FLD_ORG_TX_DATE err [%s]\n",data_YYMMDD);
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_ORG_TX_TIME + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,time_HHMMSS,6) < 0)
	{
		printf("Fchg32  SYS_FLD_ORG_TX_DATE err [%s]\n",data_YYMMDD);
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_ORG_TX_CYCLE + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,data_YYMMDD,8) < 0)
	{
		printf("Fchg32  SYS_FLD_ORG_TX_CYCLE err [%s]\n",data_YYMMDD);
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_ORG_TX_LOGNO + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"000481",8) < 0)
	{
		printf("Fchg32  SYS_FLD_ORG_TX_LOGNO err [%s]\n","000481");
		return -1;
	}	
	
	Fml32Id = Fmkfldid32(FLD_STRING, TX_APP_USER + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"bmkushua",8) < 0)
	{
		printf("Fchg32  TX_APP_USER err [%s]\n","bmkushua");
		return -1;
	}
	
	Fml32Id = Fmkfldid32(FLD_STRING, TX_USER_ID + baseId );
	if(Fchg32(sndBuf, Fml32Id, Fml32Oc,"bmkushua",8) < 0)
	{
		printf("Fchg32  TX_APP_USER err [%s]\n","bmkushua");
		return -1;
	}	
	return 0;
	
}
//组织报文
int  BuildSendBuffer(char *file)
{
	FILE      *fp;
	int       res,tag,found,found2;
	char      line[1024];
	char      fldName[33];
	int       fldId;
	char      fldValue[768];
	char      *pline;
	char	  *delim = "&";
	char	  *delim_str=NULL;
	int		  report_length;
	int		  Sres;
	/*报文域字段定义*/
	char		servcode[6+1];		//交易名称 譬如：100034 
	char		cardno[32+1];		//卡号
	char		cardtype[2]; 		//卡类型 1 储蓄卡 2 信用卡
	char		bankname[128];		//银行名称
	char		car_isvalid[2];		//卡有效期
	double		amount;
	//char		jiami[256+1]="864710db13eaf54e6976eeea4767f82ba1eb3dc8722ce36c57702ecbef66e7beda6b4e2708f506671a61892889bee590d62339e501c47d54fab41756167b9bc19a6283f024cf12877eb939f0693349635e9eab83393690d9a35370582380ccd1c3493a8e787a78ba3d8e10212706c4eb7637be03a4ed44cb3aa0a301beca283b";
	
	unsigned char		jiami[256+1]="29D38EDEF0F5F8ED8D55019585A4917E757AB0BCBA0361BC4968BD30CCE60FF46AD0908A9E26302039193335D4EFF3702EE48D7665C368763E0C7E4DBF355A4B01CF2579FB5C77BE4D8F1DFACAA01842FBA632CA3E41599C8660280C13DDC5E333E780C667FFEAE7A637A3E9D42E7999C410042EF69BCE0CCB98A9F55446389C";
	/*报文域字段定义*/
	
	//初始化该FML缓冲区
	Finit32( sndBuf, Fsizeof(sndBuf) );

	/*读取报文文件*/
	fp = fopen(file,"r");
	if ( !fp )
	{
	   printf("资源文件(%s)打不开\n",file);
	   return -1;
	}
	memset(line, 0, sizeof(line));
	report_length = 0;
	if((pline = fgets(line, 1024, fp)) != NULL)
	{
		printf("report.txt=[%s]\n",line);
		//获取servcode
		memset(servcode,0,sizeof(servcode));
		memcpy(servcode,line,6);
		printf("servcode=[%s]\n",servcode);
		Fml32Id = Fmkfldid32(FLD_STRING, SYS_FLD_SERVICE_CODE + baseId );
		Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,servcode, strlen(servcode));
		if(Sres < 0)
		{
			printf("Fchg32 err [%s] res = [%d]\n",servcode,Sres);
			return Sres;
		}		
		report_length+=6+1;
		//根据servcode获取报文
		Sres = set_key_data();
		if(Sres !=0)
		{
			printf("set_key_data err\n");
			return -1;
		}
		if(memcmp(servcode,"100090",6)==0)
		{
			//获取账号
			printf("%s\n",strtok(line,delim));
			memset(cardno,0,sizeof(cardno));
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get cardno err\n");
				return -1;	
			}
			printf("servcode = [%s]\n",delim_str);
			memcpy(cardno,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, TX_ACCOUNT + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,cardno,strlen(cardno));
			if(Sres < 0)
			{
				printf("Fchg32 err [%s] res = [%d]\n",cardno,Sres);
				return Sres;
			}
			
			Fml32Id = Fmkfldid32(FLD_STRING, TX_PRODUCT_ID + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,"0000000000", 10);
			if(Sres < 0)
			{
				printf("Fchg32 TX_PRODUCT_ID err [%s] res = [%d]\n",Sres);
				return Sres;
			}
			
			Fml32Id = Fmkfldid32(FLD_STRING, TX_MERCHANT_ID + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,"0004000000", 10);
			if(Sres < 0)
			{
				printf("Fchg32 TX_MERCHANT_ID err res = [%d]\n",Sres);
				return Sres;
			}			
		}
		if(memcmp(servcode,"100093",6)==0)
		{
			
			memset(&out_100093,0,sizeof(out_100093));
			//获取账号
			printf("%s",strtok(line,delim));
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get cardno err\n");
				return -1;	
			}
			memcpy(out_100093.cardno,delim_str,strlen(delim_str));
			printf("out_100093.cardno = [%s]\n",out_100093.cardno);
			
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[0] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.cardno,strlen(out_100093.cardno));
			if(Sres < 0)
			{
				printf("Fchg32 err [%s] res = [%d]\n",cardno,Sres);
				return Sres;
			}
			//获取卡有效期
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get car_ex_date err\n");
				return -1;	
			}
			memcpy(out_100093.car_ex_date,delim_str,strlen(delim_str));
			printf("out_100093.car_ex_date = [%s]\n",out_100093.car_ex_date);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[1] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.car_ex_date,strlen(out_100093.car_ex_date));
			if(Sres < 0)
			{
				printf("Fchg32 err [car_ex_date] res = [%d]\n",Sres);
				return Sres;
			}	
			//获取持卡人姓名
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get customer_name err\n");
				return -1;	
			}
			memcpy(out_100093.customer_name,delim_str,strlen(delim_str));
			printf("out_100093.customer_name = [%s]\n",out_100093.customer_name);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[2] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.customer_name,strlen(out_100093.customer_name));
			if(Sres < 0)
			{
				printf("Fchg32 err [customer_name] res = [%d]\n",Sres);
				return Sres;
			}
			//获取证件类型
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get pid_type err\n");
				return -1;	
			}
			memcpy(out_100093.pid_type,delim_str,strlen(delim_str));
			printf("out_100093.pid_type = [%s]\n",out_100093.pid_type);
			
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[3] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.pid_type,strlen(out_100093.pid_type));
			if(Sres < 0)
			{
				printf("Fchg32 err [customer_name] res = [%d]\n",Sres);
				return Sres;
			}
			//证件号
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get customer_pid err\n");
				return -1;	
			}
			memcpy(out_100093.customer_pid,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[4] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.customer_pid,strlen(out_100093.customer_pid));
			if(Sres < 0)
			{
				printf("Fchg32 err [customer_name] res = [%d]\n",Sres);
				return Sres;
			}			
			//卡背后三位数字
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get card_num2 err\n");
				return -1;	
			}
			memcpy(out_100093.card_num2,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[5] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.card_num2,strlen(out_100093.card_num2));
			if(Sres < 0)
			{
				printf("Fchg32 err [customer_name] res = [%d]\n",Sres);
				return Sres;
			}
						/*static int did_100093[]={64,338,191,226,227,248,129,87,82,343,66,150,74,216,224,225,340,229,339};*/			
			//手机号
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get mobile_no err\n");
				return -1;	
			}
			memcpy(out_100093.mobile_no,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[6] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.mobile_no,strlen(out_100093.mobile_no));
			if(Sres < 0)
			{
				printf("Fchg32 err [mobile_no] res = [%d]\n",Sres);
				return Sres;
			}
			//客户订单号
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get order_no err\n");
				return -1;	
			}
			memcpy(out_100093.order_no,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[7] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.order_no,strlen(out_100093.order_no));
			if(Sres < 0)
			{
				printf("Fchg32 err [mobile_no] res = [%d]\n",Sres);
				return Sres;
			}				
			//交易时间
			memcpy(out_100093.time,time_YMDHMS,14);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[8] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.time,strlen(out_100093.time));
			if(Sres < 0)
			{
				printf("Fchg32 err [mobile_no] res = [%d]\n",Sres);
				return Sres;
			}				
			//交易币种
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get amt_type err\n");
				return -1;	
			}
			memcpy(out_100093.amt_type,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[9] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.amt_type,strlen(out_100093.amt_type));
			if(Sres < 0)
			{
				printf("Fchg32 err [amt_type] res = [%d]\n",Sres);
				return Sres;
			}				 
			//交易金额
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get amount err\n");
				return -1;	
			}
			
			//out_100093.amount = atof(delim_str);
			memcpy(out_100093.amount,delim_str,strlen(delim_str));
			printf("amount = [%s]\n",out_100093.amount);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[10] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.amount,strlen(out_100093.amount));
			if(Sres < 0)
			{
				printf("Fchg32 err [amt_type] res = [%d]\n",Sres);
				return Sres;
			}			
			//商品类别码
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get mb_product_type err\n");
				return -1;	
			}
			memcpy(out_100093.mb_product_type,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[11] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.mb_product_type,strlen(out_100093.mb_product_type));
			if(Sres < 0)
			{
				printf("Fchg32 err [mb_product_type] res = [%d]\n",Sres);
				return Sres;
			}				  
			//商品名称
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get mb_product_msg err\n");
				return -1;	
			}
			memcpy(out_100093.mb_product_msg,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[12] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.mb_product_msg,strlen(out_100093.mb_product_msg));
			if(Sres < 0)
			{
				printf("Fchg32 err [mb_product_msg] res = [%d]\n",Sres);
				return Sres;
			}			 
			//商品描述
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get mb_product_detail err\n");
				return -1;	
			}
			memcpy(out_100093.mb_product_detail,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[13] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.mb_product_detail,strlen(out_100093.mb_product_detail));
			if(Sres < 0)
			{
				printf("Fchg32 err [mb_product_detail] res = [%d]\n",Sres);
				return Sres;
			}					  
			//用户标识
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get customerid err\n");
				return -1;	
			}
			memcpy(out_100093.customerid,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[14] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.customerid,strlen(out_100093.customerid));
			if(Sres < 0)
			{
				printf("Fchg32 err [mb_product_detail] res = [%d]\n",Sres);
				return Sres;
			}	  
			//用户标识类型
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get customerid_type err\n");
				return -1;	
			}
			memcpy(out_100093.customerid_type,delim_str,strlen(delim_str));	
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[15] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.customerid_type,strlen(out_100093.customerid_type));
			if(Sres < 0)
			{
				printf("Fchg32 err [customerid_type] res = [%d]\n",Sres);
				return Sres;
			}  
			//终端标识类型
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get terminal_type err\n");
				return -1;	
			}
			memcpy(out_100093.terminal_type,delim_str,strlen(delim_str));
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[16] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.terminal_type,strlen(out_100093.terminal_type));
			if(Sres < 0)
			{
				printf("Fchg32 err [terminal_type] res = [%d]\n",Sres);
				return Sres;
			}  
			//终端标识
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get terminal_id err\n");
				return -1;	
			}
			memcpy(out_100093.terminal_id,delim_str,strlen(delim_str));	
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[17] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.terminal_id,strlen(out_100093.terminal_id));
			if(Sres < 0)
			{
				printf("Fchg32 err [terminal_id] res = [%d]\n",Sres);
				return Sres;
			}   
			//用户IP
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get ip err\n");
				return -1;	
			}
			memcpy(out_100093.ip,delim_str,strlen(delim_str));	  
			printf("ip=[%s]\n",out_100093.ip);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100093[18] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100093.ip,strlen(out_100093.ip));
			if(Sres < 0)
			{
				printf("Fchg32 err [ip] res = [%d]\n",Sres);
				return Sres;
			}
			Fml32Id = Fmkfldid32(FLD_STRING, TX_PRODUCT_ID + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,"0000000000", 10);
			if(Sres < 0)
			{
				printf("Fchg32 TX_PRODUCT_ID err [%s] res = [%d]\n",Sres);
				return Sres;
			}	
			Fml32Id = Fmkfldid32(FLD_STRING, 94 + baseId);
			Sres = Fchg32(sndBuf, Fml32Id,Fml32Oc,jiami,256);
			if(Sres < 0)
			{
				printf("Fchg32 err [car_ex_date] res = [%d]\n",Sres);
				return Sres;
			}					
		}
		if(memcmp(servcode,"100091",6)==0)
		{
			memset(&out_100091,0,sizeof(out_100091));
			printf("servcode= [%s]",strtok(line,delim));
			//获取订单号
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get order_no err\n");
				return -1;	
			}
			memcpy(out_100091.order_no,delim_str,strlen(delim_str));
			printf("out_100091.order_no=[%s]\n",out_100091.order_no);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100091[0] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100091.order_no,strlen(out_100093.order_no));
			if(Sres < 0)
			{
				printf("Fchg32 err [%s] res = [%d]\n",cardno,Sres);
				return Sres;
			}
			Fml32Id = Fmkfldid32(FLD_STRING, TX_MERCHANT_ID + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,"0004000001", 10);
			if(Sres < 0)
			{
				printf("Fchg32 TX_MERCHANT_ID err res = [%d]\n",Sres);
				return Sres;
			}				
		}
		if(memcmp(servcode,"100092",6)==0)
		{
			memset(&out_100092,0,sizeof(out_100092));
			printf("servcode= [%s]",strtok(line,delim));
			//获取订单号
			delim_str = strtok(NULL,delim);
			if(delim_str==NULL)
			{
				printf("get order_no err\n");
				return -1;	
			}
			memcpy(out_100092.order_no,delim_str,strlen(delim_str));
			printf("out_100092.order_no=[%s]\n",out_100092.order_no);
			Fml32Id = Fmkfldid32(FLD_STRING, did_100092[0] + baseId);
			Sres = Fchg32(sndBuf, Fml32Id, Fml32Oc,out_100092.order_no,strlen(out_100092.order_no));
			if(Sres < 0)
			{
				printf("Fchg32 err [%s] res = [%d]\n",cardno,Sres);
				return Sres;
			}			
		}
	}else{
		printf("report.txt=[%s]\n",line);
		return -1;	
	}
	fclose(fp);
	
	return 0;
}

//解析应答报文
int GetRecvmessage()
{
	int 		Sres;
	char 	  	Fml32Buf[2048];
	int       	Fml32Len;
	int       	DataItemId;
	char		servcode[6+1];		//交易名称 譬如：100090 
	in_100090 = malloc(sizeof(MY_YB_100090_IN));
	memset(in_100090,0,sizeof(MY_YB_100090_IN));
	char	Sbank_name[64];
   	Fml32Id = FIRSTFLDID;
   	Fml32Len = 0;
   	printf("应答报文:\n");
   	Fprint32(rcvBuf);
   	for(Sres = 1;Sres == 1;)
  	{
  		Fml32Len = sizeof(Fml32Buf);
      	memset(Fml32Buf, 0, Fml32Len);
    	Sres = Fnext32(rcvBuf, &Fml32Id, &Fml32Oc, Fml32Buf, (FLDLEN32 *)&Fml32Len);
      	if(Sres == 0)
      	{
      		printf("******rcvBuf end\n");
         	break;
      	}else if(Sres < 0)
      	{
      		printf("Fnext32 err \n");
      		break;	
      	}
      	Fml32Len--;
		DataItemId  = (int)Fldno32(Fml32Id) - baseId;
		printf("(int)Fldno32(Fml32Id) = [%d] DataItemId =[%d] [%s]\n",(int)Fldno32(Fml32Id),DataItemId,Fml32Buf);
		memset(Sbank_name,0,sizeof(Sbank_name));

		switch(DataItemId)
		{
			case SYS_FLD_SERVICE_CODE:
				memset(servcode,0,sizeof(servcode));
				memcpy(servcode,Fml32Buf,Fml32Len);
				break;
			case TX_BANK_NAME:
				memcpy(Sbank_name,Fml32Buf,Fml32Len);
				HEXTOSTR(Sbank_name,in_100090->bank_name);
				break;
			case TX_PAY_FLAG:
				memcpy(in_100090->card_isvalid,Fml32Buf,Fml32Len);
				break;
			case TX_CARD_TYPE:
				memcpy(in_100090->cardtype,Fml32Buf,Fml32Len);
				break;	
			case TX_ACCOUNT:
				memcpy(in_100090->cardno,Fml32Buf,Fml32Len);
				break;							
			default:
				break;
		}
	}
	printf("servcode=[%s]\n",servcode);
	if(memcmp(servcode,"100090",6)==0)
	{
		printf("卡    号 :   [%s]\n",in_100090->cardno);	
		printf("卡 类 型 ：  [%s]\n",in_100090->cardtype);
		printf("卡有效期 :   [%s]\n",in_100090->card_isvalid);
		printf("银行名称 :   [%s]\n",in_100090->bank_name);		
	}
	return 0;
}
void vmcallexit()
{
	if ( FLAG )
	{
		if ( sndBuf )
		{
		   tpfree((char *)sndBuf);
		   sndBuf = NULL;
		}
		
		if ( rcvBuf )
		{
		   tpfree((char *)rcvBuf);
		   rcvBuf = NULL;
		}
		
		tpterm();
	}
}

//信息初始化
int init()
{	
	char time_str[20];
	//初始化时间
	memset(time_str,0,sizeof(time_str));
	time(&timep);
	local_time=localtime(&timep); /*取得当地时间*/
	memset(data_YYMMDD,0,sizeof(data_YYMMDD));
	memset(time_HHMMSS,0,sizeof(time_HHMMSS));
	memset(ref_num,0,sizeof(ref_num));
	memset(time_YMDHMS,0,sizeof(time_YMDHMS));	
	sprintf(time_str,"%d%.2d%.2d%d%.2d%.2d",(1900+local_time->tm_year),( 1+local_time->tm_mon), local_time->tm_mday,local_time->tm_hour, local_time->tm_min,local_time->tm_sec);
	memcpy(data_YYMMDD,time_str,8);
	memcpy(time_HHMMSS,time_str+8,6);
	
	sprintf(time_YMDHMS,"%s%s",data_YYMMDD,time_HHMMSS);
	sprintf(ref_num,"%s%s%s",data_YYMMDD,time_HHMMSS,"000666");
	
	return 0;
}	
int main(int argc, char **argv)
{
	int  res;
	long len;
	int  connectId; 
	long revent; /* tpsend 失败的事件方式 */
	if ( argc != 2 )
	{
	   printf("Usage: Appcli	report.txt\n");
	   exit(0);
	}
	
	atexit(vmcallexit);
	
	res = init();
	if(res != 0)
	{
		printf("init err\n");
		exit(0);	
	}
	if (tpinit((TPINIT *)NULL) == -1)
	{
	   printf("调用函数 tpinit 失败 <%d><%s>\n",tperrno, tpstrerror(tperrno) );
	   return 0;
	}
	
	rcvBuf = (FBFR32 *)tpalloc(FMLTYPE32,NULL,8096);
	if ( rcvBuf == NULL )
	{
	   printf("调用函数 tpalloc 失败 <%d><%s>\n",tperrno, tpstrerror(tperrno) );
	   tpterm();
	   exit(0);
	}
	
	sndBuf = (FBFR32 *)tpalloc(FMLTYPE32,NULL,8096);
	if ( sndBuf == NULL )
	{
	   tpfree((char *)rcvBuf);
	   rcvBuf = NULL;
	   printf("调用函数 tpalloc 失败 <%d><%s>\n",tperrno, tpstrerror(tperrno) );
	   tpterm();
	   exit(0);
	}
	
	FLAG = 1;
	
	res = BuildSendBuffer(argv[1]);
	if ( res )
	{
	   printf("组包失败!!!\n");
	   exit(0);
	}
	
	printf("报文信息:\n");
	Fprint32(sndBuf);
	res = tpcall("RSPAYSERVICE", (char *)sndBuf, 0L, (char **)&rcvBuf, &len, 0);
	if ( res < 0 )
	{
	   printf("调用函数 tpcall 失败,错误信息<%d:%s>\n", tperrno, tpstrerror(tperrno) );
	   exit(0);
	}
	/*connectId = tpconnect("RSPAYSERVICE", NULL, 0, TPSENDONLY); 
	if(connectId == -1)
	{
		printf("调用函数tpconnect失败，错误信息<%d:%s>\n",tperrno, tpstrerror(tperrno));
		exit(0);
	}
	if(tpsend(connectId , (char*)sndBuf ,(long)strlen(sndBuf), 0 , &revent) == -1)
	{
		printf("调用函数tpsend失败，错误信息<%d:%s>\n",tperrno, tpstrerror(tperrno));
		exit(0);
	}*/
	/* 客户端调用TPRECVONLY 标志放弃控制权，准备接受数据*/
	/*if (tpsend(connectId , NULL , 0, TPRECVONLY , &revent) == -1)
	{
		printf("调用函数tpsend失败，错误信息<%d:%s>\n",tperrno, tpstrerror(tperrno));
		exit(0);
	}
	
	if(tprecv(connectId, (char**)&rcvBuf , &len, 0, &revent) == -1)
	{
		printf("调用函数tprecv失败，错误信息<%d:%s>\n",tperrno, tpstrerror(tperrno));
		exit(0);
	}*/
	
	
	res = GetRecvmessage();
	if(res < 0)
	{
		printf("GetRecvmessage err \n");
		exit(-1);	
	}
	exit(0);
}

//十六进制转换为汉子
int HEXTOSTR(char *hex,char *str)
{
	int hex_len;
	int	hex_val;
	unsigned char hex_str[3];
	int i,j;
	unsigned char buf[128];
	hex_len = strlen(hex);
	i=0;
	j=0;
	memset(buf,0,sizeof(buf));
	while(i<hex_len)
	{
		memset(hex_str,0,sizeof(hex_str));
		memcpy(hex_str,hex+i,2);		
		sscanf (hex_str, "%x", &hex_val);
		buf[j]=hex_val;
		i+=2;
		j++;
	}
	memcpy(str,buf,strlen(buf));
	return 0;
}

